package com.example.campustalent.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import com.example.campustalent.adapter.TabPagerAdapter
import com.example.campustalent.bean.StudyInfo
import com.example.campustalent.bean.UserInfoBean
import com.example.campustalent.databinding.FragmentStudyBinding
import com.example.campustalent.event.TabEvent
import com.example.campustalent.fragment.MeFragment.Companion.newInstance
import com.example.campustalent.utils.DbUtils
import com.example.campustalent.utils.SharedPreferencesUtils
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import java.util.*


class StudyFragment : Fragment() {
    private lateinit var binding: FragmentStudyBinding
    private var mFirst: Boolean = false
    private var mName: String? = null
    private val titleList: MutableList<String> = ArrayList()
    private lateinit var preferencesUtils: SharedPreferencesUtils
    companion object {
        fun newInstance(isFirst: Boolean, name: String): StudyFragment{
            return StudyFragment().apply {
                arguments = Bundle().apply {
                    putBoolean("isFirst", isFirst)
                    putString("name", name)
                }
            }
        }


    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        preferencesUtils = SharedPreferencesUtils(activity)
        if (DbUtils.liteOrm == null) {
            DbUtils.createDb(activity, "CampusTalent")
        }
        if (mFirst) {
            binding.llLayout.visibility = View.GONE
            binding.tvEmpty.visibility = View.VISIBLE
        } else {
           initTab(preferencesUtils.getParam("schoolPosition", "0").toString())
        }

    }

    private fun initTab(position: String){
        binding.llLayout.visibility = View.VISIBLE
        binding.tvEmpty.visibility = View.GONE
        val userInfoBean =
            DbUtils.getQueryByWhere(UserInfoBean::class.java, "name", arrayOf(mName!!))[0]
        titleList.clear()
        val fragmentList: MutableList<Fragment> = ArrayList()
        when(position){
            "0" ->{
                userInfoBean.tabContentSF?.let {
                    val typeToken: TypeToken<List<StudyInfo>> =
                        object : TypeToken<List<StudyInfo>>() {
                        }
                    val list: List<StudyInfo> =
                        Gson().fromJson(it, typeToken.type)

                    for (bean in list) {
                        titleList.add(bean.tabName!!)
//                        fragmentList.add(TestFragment.setNewInstance("test1"))
                        val fragment = PublicFragment.newInstance(bean, "0", mName!!)
                        fragmentList.add(fragment)
                    }
                }
            }
            "1" ->{

                userInfoBean.tabContentLG?.let {
                    val typeToken: TypeToken<List<StudyInfo>> =
                        object : TypeToken<List<StudyInfo>>() {
                        }
                    val list: List<StudyInfo> =
                        Gson().fromJson(it, typeToken.type)
                    for (bean in list) {
//                        fragmentList.add(TestFragment.setNewInstance("test2"))
                        titleList.add(bean.tabName!!)
                        fragmentList.add(PublicFragment.newInstance(bean,"1",  mName!!))
                    }
                }
            }
            "2" ->{
                userInfoBean.tabContentYX?.let {
                    val typeToken: TypeToken<List<StudyInfo>> =
                        object : TypeToken<List<StudyInfo>>() {
                        }
                    val list: List<StudyInfo> =
                        Gson().fromJson(it, typeToken.type)
                    for (bean in list) {
                        titleList.add(bean.tabName!!)
                        fragmentList.add(PublicFragment.newInstance(bean, "2", mName!!))
                    }
                }
            }
        }
        if (userInfoBean.tabContentLG.isNullOrBlank() && userInfoBean.tabContentSF.isNullOrBlank() && userInfoBean.tabContentYX.isNullOrBlank()){
            binding.llLayout.visibility = View.GONE
            binding.tvEmpty.visibility = View.VISIBLE
            return
        }
        setData(childFragmentManager, titleList, fragmentList)
    }
    private fun setData(supportManager: FragmentManager, titleList: MutableList<String>, fragment: List<Fragment>){
        val adapter = TabPagerAdapter(childFragmentManager, fragment, titleList)
        binding.viewPager.adapter = adapter
        binding.viewPager.offscreenPageLimit = titleList.size
        binding.tabLayout.setupWithViewPager(binding.viewPager)

    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onEventMainThread(event: TabEvent?) {
        event?.let {
            initTab(it.position)
        }
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mFirst = requireArguments().getBoolean("isFirst")
        mName = requireArguments().getString("name")
        EventBus.getDefault().register(this)
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = FragmentStudyBinding.inflate(inflater)
        return binding.root
    }

}